﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Search.Administration;

using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Logging;
using Kiiro.Model.Utility;
using Kiiro.ExceptionManagement;

using NavFields = Kiiro.Common.Utility.Constants.NavigationFieldNames;

namespace Kiiro.Model.EventHandlers
{
    public class KiiroNavigationItems : SPFeatureReceiver
    {
        public override void FeatureActivated ( SPFeatureReceiverProperties properties )
        {
            SPWeb web = properties.Feature.Parent as SPWeb;

            try
            {
                SPList list = web.Lists [ Constants.SPListNames.MainNavigation ];
                if ( list != null )
                    list.Delete ( );
            }
            catch { }

            try
            {
                SPList list = web.Lists [ Constants.SPListNames.SubNavigation ];
                if ( list != null )
                    list.Delete ( );
            }
            catch { }

            web.Lists.Add ( "Kiiro Main Navigation", "Kiiro Main Navigation List", "Lists/MainNavigation", "2506CDB8-FB9D-4d96-A81E-5BEC418EE1C7", 50100, "" );
            web.Lists.Add ( "Kiiro Sub Navigation", "Kiiro Sub Navigation List", "Lists/SubNavigation", "2506CDB8-FB9D-4d96-A81E-5BEC418EE1C7", 50101, "" );

            CreateNavInstanceData ( web, Constants.SPListNames.MainNavigation, Constants.NavigationItems.MainNavItems );
            CreateNavInstanceData ( web, Constants.SPListNames.SubNavigation, Constants.NavigationItems.SubNavItems );

            web.Properties [ "IsNavigationProvisioned" ] = bool.TrueString;
            web.Properties.Update ( );
        }


        private void CreateNavInstanceData ( SPWeb web, string listName, List<NavigationItem> navItems )
        {
            try
            {
                SPList list = web.Lists [ listName ];

                foreach ( NavigationItem navItem in navItems )
                {
                    SPListItem listItem = list.Items.Add ( );

                    foreach ( string display in navItem.PageDisplayCollection )
                        listItem [ NavFields.PageDisplayCollection ] += display + "\r\n";

                    foreach ( string hilite in navItem.PageHiLiteCollection )
                        listItem [ NavFields.PageHiLiteCollection ] += hilite + "\r\n";

                    listItem [ NavFields.GroupVisibility ] = FormattingHelpers.ListOfKiiroGroupsToFieldText ( navItem.GroupVisibility );

                    listItem [ NavFields.IsVisible ] = navItem.IsVisible;
                    listItem [ NavFields.Sequence ] = navItem.Sequence;
                    listItem [ NavFields.SiteLevel ] = navItem.SiteLevel;
                    listItem [ NavFields.TargetUrl ] = navItem.TargetUrl;
                    listItem [ NavFields.Title ] = navItem.Title;

                    listItem.SystemUpdate ( );
                }
            }
            catch ( Exception ex )
            {
                ExceptionManager.HandleException ( ex, false );
            }
        }

        public override void FeatureDeactivating ( SPFeatureReceiverProperties properties ) { }

        public override void FeatureInstalled ( SPFeatureReceiverProperties properties ) { }

        public override void FeatureUninstalling ( SPFeatureReceiverProperties properties ) { }
    }
}
/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/EventHandlers/FeatureEventHandlers/KiiroNavigationItems.cs $
 * 
 * 2     2/24/10 4:49p Camerons
 * 
 * 1     1/11/10 11:51a Camerons
 * 
 ************************************************************************/